package chances.epg.navigator.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import chances.epg.data.service.BizPositionService;
import chances.epg.entity.biz.BizPositionItem;
import chances.epg.entity.content.CategoryItem;
import chances.epg.entity.content.SubjectPage;
import chances.epg.navigator.AccessException;
import chances.epg.navigator.context.AccessContext;


/**
 * 编排元素访问
 * @author Elysium
 *
 */
@Controller
public class ItemAccessController extends AccessController {
	@Autowired
	protected BizPositionService bizPositionService;
	
	@RequestMapping(value = "/{siteCode}/{bizCode}/{containerType}/{containerCode}/item/{itemType}/{itemId}")
	public String item(ModelMap model, HttpServletRequest request,HttpServletResponse response) throws IOException {
		AccessContext context = accessContextFactory.createAccessContext(request);
		
		if(context.isCateoryContainer()){
			return this.categoryItem(context,model,request,response);
		}else if(context.isPositionContainer()){
			return this.positionItem(context,model,request,response);
		}else{
			logger.error("Not support container type,uri:{}",request.getRequestURI());
			throw new AccessException(AccessException.ACC_INVALID_CONTAINER,"Not support container type:" + context.getContainerType());
		}
	}
	
	/**
	 * 专题中的外链规则跳转支持
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/{siteCode}/{bizCode}/{containerType}/{containerCode}/link/{contentCode}")
	public String link(ModelMap model, HttpServletRequest request, 
			HttpServletResponse response) throws IOException {
		AccessContext context = accessContextFactory.createAccessContext(request);
		context.setContentType(CategoryItem.ITEMTYPE_URL);
		try {
			return this.linkItem(context, model, request, response);
		} catch (Exception e) {
			logger.error("Not support container type,uri:{}",request.getRequestURI());
			throw new AccessException(AccessException.ACC_INVALID_CONTAINER,"Not support container type:" + context.getContainerType());
		}
	}
	
	/**
	 * 外链
	 * 
	 * @param context
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	private String linkItem(AccessContext context, ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		context.setContentType(context.getContentType());
		context.setContentCode(context.getContentCode());
		return detail(model, request, context);
	}

	/**
	 * 编排单.
	 * @param model
	 * @param request
	 * @param context
	 * @return
	 * @throws IOException 
	 */
	private String categoryItem(AccessContext context,ModelMap model, HttpServletRequest request,HttpServletResponse response) throws IOException{
		CategoryItem categoryItem = this.contentService.getCateogryItemById(context.getItemId());
		if(categoryItem == null) {
			logger.error("item not founded,uri:{}",request.getRequestURI());
			throw new AccessException(AccessException.ACC_ITEM_NOT_FOUND,"item not found,item id:" + context.getItemId());
		}
		
		context.setContentType(categoryItem.getItemType());
		context.setContentCode(categoryItem.getItemCode());
		
		if(!categoryItem.getCategoryCode().equals(context.getContainerCode())){
			logger.error("container code is illegal,uri:{}",request.getRequestURI());
			throw new AccessException(AccessException.ACC_ILLEGAL_CONTAINER,"container code is illegal.");
		}
		if(categoryItem.isSubject()){
			this.redirectSubject(context, request, response, categoryItem.getItemCode());
			return null;
		}
		if(categoryItem.isBiz()){
			this.redirectBiz(context, request,response, categoryItem.getItemCode());
			return null;
		}else if(categoryItem.isContent()){
			return detail(model, request, context);
		}else{
			logger.error("invalid item type,uri:{}",request.getRequestURI());
			throw new AccessException(AccessException.ACC_INVALID_ITEMTYPE,"invalid item type");
		}
	}


	/**
	 * 推荐位元素.
	 * @param model
	 * @param request
	 * @param context
	 * @return
	 */
	private String positionItem(AccessContext context,ModelMap model, HttpServletRequest request,HttpServletResponse response) throws IOException {
		BizPositionItem positionItem = this.bizPositionService.getPositionItemById(context.getItemId());
		
		if(positionItem == null) {
			logger.error("position item not founded,uri:{}",request.getRequestURI());
			throw new AccessException(AccessException.ACC_POSITION_NOT_FOUND,"position item not found,position id:" + context.getItemId());
		}
		
		context.setContentType(positionItem.getObjType());
		context.setContentCode(positionItem.getObjValue());
		
		if(CategoryItem.ITEMTYPE_SUBJECT.equals(positionItem.getObjType())) {
			SubjectPage subjectPage = this.contentService.getDefaultPageOfSubject(positionItem.getObjValue());
			context.setContentCode(subjectPage.getPageCode());
		}
		
		if(positionItem.isBiz()){
			this.redirectBiz(context, request,response, positionItem.getObjValue());
			return null;
		}else {
			return detail(model, request, context);
		}
	}
}
